/**
                                                                                                                                  * Created by lanyanhua on 2018/10/31 0031.
 */
NProgress.start();
window.onload = function () {
    NProgress.done();
};
layui.use(['element', 'table', 'form', 'jquery', 'laydate','list'], function () {
    var table = layui.table;
    var form = layui.form;
    var $ = layui.jquery;
    var list = layui.list;
    var element = layui.element;
    //数据表格
    var showdatatable = table.render({
        elem: '#tableId',
        id:'showdata',
        url: '/device/findDevicePage',
        limit: 10,
        page: true,
        while: param(),
        cols: [ [
            {type: 'checkbox'},
            {field: 'deviceId', title: 'ID', align: 'center', sort: true ,width: 60},
            {field: 'deviceName', title: '设备名称', align: 'center',width: 190},
            {field: 'deviceCode', title: '设备编号', align: 'center',width: 110},
            {field: 'status', title: '设备状态', align: 'center',width: 80,
            	templet:function (d){
            		if('1' == d.status){
            			return "正常";
            		}else{
            			return "异常";
            		}
            	}
            },
            {field: 'remark2', title: '门派', align: 'center',width: 190},
            {title: '操作', templet: '#operationTpl',width: 590, align: 'center',fixed:'right'}
        ] ],
        done: function (res, curr, count) {
            $("#countNum").text(count);
        }
    });
    //list.open(options); 表单配置
    var listOpen  = {
        elem:"#groupAdd",
        fields:{
        	deviceId:{label:'ID',type:'hidden'},
        	deviceName:{label: '设备名称'},
        	deviceCode : {label:"设备编码"},
        	status : {label:"状态"},
        	ipAddr : {label:"IP地址"},
        	tcpPort : {label:"TCP端口"},
        	forwardPort : {label:"FOR端口"},
        	phone : {label:"手机号码"},
        	belong : {label:"归属"},
        	remark1:{label:"备注1"},
        	remark2:{label:"备注2"}
        },
        submit : function(res){ //表单提交事件
            $.ajax({
                type : 'post',
                url : '/device/saveDevice',
                data : res.field ,
                dataType : 'json',
                success : function (data) {
                    if(data.success){
                        layer.msg(data.message, {icon: 1, time: 1000});
                        var index = layer.getFrameIndex(window.name);
                        layer.close(index); //再执行关闭
                        layui.table.reload('showdata',{page:{curr:1}});
                    }else{
                        layer.msg(data.message, {icon: 2, time: 5000});
                    }
                }
            });
            return false;
        },
        btns:["submit",'reset'],
        area:['70%','95%']
    };
    // 提交时间
    form.on('submit(search)', function () {
        showdatatable.reload({where: param()});
        return false;
    });
    // 编辑按钮 ， 删除按钮
    table.on('tool(tableFilter)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'read') {
        	layer.confirm("确定开始读取宝图数据吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function (index) {
        		layer.close(index);
        		var DISABLED = 'layui-btn-disabled';
            	$('#'+data.deviceId).addClass(DISABLED);
        		$.post('/mhxy/readTuDate',{deviceCode:data.deviceCode},function (data) {
        			$('#'+data.deviceId).removeClass(DISABLED);
                    if(data.success){
                        layer.msg(data.message, {icon: 1, time: 1000});
                        //layui.table.reload('showdata',{page:{curr:1}});
                    }else{
                        layer.msg(data.message, {icon: 2, time: 5000});
                    }
                })
            });
        } 
        if (layEvent === 'wa') {
        	layer.confirm("确定要开始挖宝图吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function (index) {
        		layer.close(index);
        		var DISABLED = 'layui-btn-disabled';
            	$('#'+data.deviceCode).addClass(DISABLED);
                $.post('/mhxy/dt',{deviceCode:data.deviceCode},function (data) {
                	$('#'+data.deviceCode).removeClass(DISABLED);
                    if(data.success){
                        layer.msg(data.message, {icon: 1, time: 1000});
                        //layui.table.reload('showdata',{page:{curr:1}});
                    }else{
                        layer.msg(data.message, {icon: 2, time: 5000});
                    }
                })
            });
        } 
        
        if (layEvent === 'sm') {
        	layer.confirm("确定要开始师门任务吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function (index) {
        		layer.close(index);
        		var DISABLED = 'layui-btn-disabled';
            	$('#'+data.deviceCode).addClass(DISABLED);
                $.post('/mhxySmTask/smTask',{deviceCode:data.deviceCode},function (data) {
                	$('#'+data.deviceCode).removeClass(DISABLED);
                    if(data.success){
                        layer.msg(data.message, {icon: 1, time: 1000});
                        //layui.table.reload('showdata',{page:{curr:1}});
                    }else{
                        layer.msg(data.message, {icon: 2, time: 5000});
                    }
                })
            });
        } 
        
        if (layEvent === '100sm') {
        	layer.confirm("确定要开始100师门任务吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function (index) {
        		layer.close(index);
        		var DISABLED = 'layui-btn-disabled';
            	$('#'+data.deviceCode).addClass(DISABLED);
                $.post('/mhxyHundrSmTask/hundrSmTask',{deviceCode:data.deviceCode,remark2:data.remark2},function (data) {
                	$('#'+data.deviceCode).removeClass(DISABLED);
                    if(data.success){
                        layer.msg(data.message, {icon: 1, time: 1000});
                        //layui.table.reload('showdata',{page:{curr:1}});
                    }else{
                        layer.msg(data.message, {icon: 2, time: 5000});
                    }
                })
            });
        }
        
        if (layEvent === 'edit') {
            //console.log("编辑");
            listOpen.over = data;
            list.open(listOpen);
        } else if (layEvent === 'del') {
            layer.confirm("确定要执行吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function () {
                $.post('/mhxy/dt',{deviceCode:data.deviceCode},function (data) {
                    if(data.success){
                        layer.msg(data.message, {icon: 1, time: 1000});
                        layui.table.reload('showdata',{page:{curr:1}});
                    }else{
                        layer.msg(data.message, {icon: 2, time: 5000});
                    }
                })
            });
        }
    });
    // 添加时间
    $("#addGroup").click(function(){
        listOpen.over = null;
        //弹出框 
        list.open(listOpen);
    });
    
    
    function  param() {
        return {
        	deviceName: $('[name=deviceName]').val(),
        	deviceCode: $('[name=deviceCode]').val(),
        	ipAddr: $('[name=ipAddr]').val(),
        	phone: $('[name=phone]').val()
        }
    }
});
